package com.zhuiYun.project.api.sysUser.controller;

import com.zhuiYun.project.annotation.OptLog;
import com.zhuiYun.project.api.sysUser.entiy.SysUser;
import com.zhuiYun.project.api.sysUser.model.SysUserModel;
import com.zhuiYun.project.api.sysUser.service.SysUserService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.UserUtils;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName SysUserController
 * @Description 用户管理 用于系统的登录  /login 默认登录地址 logout 登出地址
 * @Author zcy
 * @Date 2023/3/28 14:17
 **/
@RestController
@Api(tags = "用户管理")
@RequestMapping("/sysUser")
@Slf4j
public class SysUserController extends BaseController {

    @Autowired
    SysUserService sysUserService;

    /**
     * @description:根据用户查询权限
     * @author: dongdong
     * @date: 2023/4/3 20:01
     * @param:
     * @return: com.zhuiYun.project.common.response.CommonResult
     **/
    @GetMapping("/getPowerByUser")
    @ApiOperation(value = "根据用户查询权限")
    @OptLog(modular = "用户", type = CommonConstants.SELECT, describe = "根据用户查询权限")
    public CommonResult getPowerByUser()  {
        return sysUserService.getPowerByUser();
    }
    /**
     * 保存用户
     * @param sysUserModel
     * @return
     */
    @PostMapping("/saveUser")
    @ApiOperation(value = "用户管理的新增方法")
    @OptLog(modular = "用户管理", type = CommonConstants.ADD, describe = "新增用户")
    public CommonResult saveUser(SysUserModel sysUserModel, MultipartFile avatar) throws CommonException {
        log.info("新增用户");
        return sysUserService.saveUser(sysUserModel,avatar);
    }
    /**
     * 修改用户信息
     * @param sysUserModel
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation(value = "用户管理的修改方法")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "修改用户")
    public CommonResult updateUser(SysUserModel sysUserModel, MultipartFile avatar) throws CommonException {
        log.info("修改用户");
        return sysUserService.updateUser(sysUserModel,avatar);
    }
    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/delUserById")
    @ApiOperation(value = "用户管理的删除方法")
    @OptLog(modular = "用户管理", type = CommonConstants.DELETE, describe = "删除用户")
    public CommonResult delUserById(Integer id) throws CommonException {
        log.info("删除用户信息");
        return sysUserService.delUserById(id);
    }
    /**
     * 查询用户信息
     * @param sysUserModel
     * @return
     */
    @GetMapping("/getUserByPage")
    @ApiOperation(value = "用户管理的分页查询方法")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "分页查询")
    public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) throws CommonException {
        log.info("分页查询");
        return sysUserService.getUserByPage(sysUserModel,pageRequest);
    }
    /**
     * @description:获取员工登录信息
     * @author: dongdong
     * @date: 2023/4/6 10:05
     * @param: []
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @GetMapping("/getLoginUser")
    @ApiOperation(value = "获取员工登录信息")
    @OptLog(modular = "后台管理", type = CommonConstants.SELECT, describe = "获取员工登录信息")
    public CommonResult getLoginUser () throws CommonException{
        return CommonResult.ok(UserUtils.getUser());
    }


    /**
     * 查询注销用户信息
     * @param sysUserModel
     * @return
     */
    @GetMapping("/getUserRetrieveByPage")
    @ApiOperation(value = "用户管理的查询注销用户信息")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "查询注销用户信息")
    public CommonResult getUserRetrieveByPage(SysUserModel sysUserModel, PageRequest pageRequest) throws CommonException {
        log.info("分页查询");
        return sysUserService.getUserRetrieveByPage(sysUserModel,pageRequest);
    }
    /**
     * @description:给用户授权
     * @author: dongdong
     * @date: 2023/4/6 20:02
     * @param: [userId, menuIds]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @PostMapping("/userPower")
    @ApiOperation(value = "给用户授权")
    @OptLog(modular = "用户管理", type = CommonConstants.UPDATE, describe = "给用户授权")
    public CommonResult userPower(Integer userId , Integer[] menuIds){
        List<Integer> integers = new ArrayList<>();
        if(menuIds!=null){

             integers = Arrays.asList(menuIds);
        }
        return sysUserService.userPower(userId,integers);
    }
    /**
     * @description:查询用户权限
     * @author: dongdong
     * @date: 2023/4/6 20:58
     * @param: [userId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @GetMapping("getUserPower")
    @ApiOperation(value = "查询用户权限")
    @OptLog(modular = "用户管理", type = CommonConstants.SELECT, describe = "查询用户权限")
    public CommonResult getUserPower(Integer userId){
        return sysUserService.getUserPower(userId);
    }



    /**
     * @Author 小江
     * @Description //TODO
     * @Date 10:33 2023/4/26
     **/
    @GetMapping("getUserById")
    @ApiOperation(value = "获取后台管理客服账号信息")
    @OptLog(modular = "获取后台管理客服账号信息", type = CommonConstants.SELECT, describe = "获取后台管理客服账号信息")
    public CommonResult getUserById(Integer id){
        return sysUserService.getUserById(id);
    }

//    /**
//     * @Author 小江
//     * @Description //TODO
//     * @Date 10:33 2023/4/26
//     **/
//    @GetMapping("getUserById")
//    @ApiOperation(value = "根据用户id获取门户user用户信息")
//    @OptLog(modular = "根据用户id获取门户user用户信息", type = CommonConstants.SELECT, describe = "根据用户id获取门户user用户信息")
//    public CommonResult getUserById(Integer id){
//        return sysUserService.getUserById(id);
//    }


}
